<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script type="text/javascript" src="swfobject.js"></script>
<script type="text/javascript" src="../../src/jSocket.js"></script>
<!--<script type="text/javascript" src="../../src/jSocket.advanced.js"></script>-->
<title>jSocket PHP server test</title>
<script type="text/javascript">
	var ping, socket = new jSocket(
		function() {
			document.getElementById('debug').innerHTML += 'On Ready!<br />\n';
			document.getElementById('debug').innerHTML += 'Connecting to '+location.host+' on port '+5011+'<br />\n';
			this.connect('localhost', 5011);
		},
		function(success, error) {
			if(success) {
				document.getElementById('debug').innerHTML += 'Succesfully connected!<br />\n';
				var d = new Date();
				ping = d.getTime();
				writeData('PING');
			} else {
				document.getElementById('debug').innerHTML += 'Could not connect: '+error+'<br />\n';
			}
	    },
	    function(data) {
	    	//document.getElementById('debug').innerHTML += 'Recieved data: '+readData(data)+'<br />\n';
			var d = new Date();
	    	document.getElementById('ping').innerHTML = (d.getTime() - ping);

	    	var fn = function(){
	    		var d = new Date();
				ping = d.getTime();
		    	writeData('PING');
	    	};

	    	setTimeout(fn, 1000);
	    },
	    function() {
	    	document.getElementById('debug').innerHTML += 'Disconnected!<br />\n';
	    }
	);

	function writeData(data) {
		if(socket.writeUTFBytes) {
			socket.writeUTFBytes(data+'\n');
			socket.flush();
		} else {
			socket.write(data+'\n');
		}
	};

	function readData(data) {
		if(socket.readUTFBytes) {
			return socket.readUTFBytes(data);
		}
		return data;
	};

	

	// On DOM ready
	window.onload = function(){
		socket.setup('xmlsocket', '../../flash/jSocket.swf');
	};
</script>
</head>
<body>
	<div id="ping"></div>
	<div id="debug"></div>
	<div id="xmlsocket"></div>
</body>
</html>